// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

#include <memory>
#include <string>

#include "coin2/exchange/base/symbology/currency.h"

namespace coin2::exchange::mexc::symbology_v3 {

static const int64_t LUNA2_LIST_TIME =
    1653609600000000000L;  // TimestampFromDatetime(2020, 05, 27);

class MexcCurrency : public base::symbology::Currency,
                     public base::symbology::CurrencyFromStrMixin<MexcCurrency> {
 public:
  using ExchangeType = base::market::ExchangeType;

 public:
  explicit MexcCurrency(const std::string& currency)
      : Currency(currency, ToNativeCurrency(currency).value(), ExchangeType::Mexc) {}

  static std::unique_ptr<MexcCurrency> FromNativeStrImpl(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    auto currency = FromNativeCurrency(native_currency_str, resolve_timestamp);
    if (currency) {
      return std::make_unique<MexcCurrency>(currency.value());
    }
    return {};
  }

  static std::unique_ptr<MexcCurrency> FromNormStrImpl(
      const std::string& currency_str,
      int64_t resolve_timestamp) {
    auto native_currency = ToNativeCurrency(currency_str);
    if (!native_currency) {
      return {};
    }

    auto norm_currency = FromNativeCurrency(*native_currency, resolve_timestamp);
    if (!norm_currency) {
      return {};
    }
    return std::make_unique<MexcCurrency>(norm_currency.value());
  }

 private:
  static std::optional<std::string> ToNativeCurrency(const std::string& currency_str) {
    if (currency_str == "BCHSV") {
      return "BSV";
    } else if (currency_str == "BCHN") {
      return "BCH";
    } else if (currency_str == "LUNA2") {
      return "LUNA";
    }

    return StringToUpperCopy(currency_str);
  }

  static std::optional<std::string> FromNativeCurrency(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    if (native_currency_str == "BSV") {
      return "BCHSV";
    } else if (native_currency_str == "BCH") {
      return "BCHN";
    } else if (native_currency_str == "LUNA" && LUNA2_LIST_TIME <= resolve_timestamp) {
      return "LUNA2";
    }

    return StringToUpperCopy(native_currency_str);
  }
};

}  // namespace coin2::exchange::mexc::symbology_v3
